<?php 
class sysPagesAction extends AdminMaster{
	public function execute($request){
		$this->data['navigation']	= __('module_systems',null,'menu').' › '. __('module_systems_action_sys_pages',null,'menu');
		$this->formData				= $request->getParameter('SysPages');	//get form data from add new || edit page
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::EDIT_RECORD:
				$this->editRecord();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list']			 		= SysPagesTable::getDataList($this->data['filter'], $this->data['pagination']);
		
		$this->data['pagination']['total']		= SysPagesTable::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'sys_pages';
		$this->data['pagination']['params']		= $this->data['filter'];
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
	}
	

	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('sysPagesForm');

		$form = new SysPagesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(SysPagesTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'sys_pages');
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
		}else{
			$data = SysPagesTable::getDataById($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->data['navigation'] .=' › '.__($data['name_key'],null,'sys_pages');
				$this->formData = array(
						'content'			=> $data['content'],
						'_csrf_token'		=> $form->getCSRFToken()
				);
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);

	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
	
}